package cn.ranko.site.service;

import cn.ranko.bean.easyui.TreeNode;
import cn.ranko.site.mapper.SysPermissionsMapper;
import cn.ranko.site.model.SysPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ZJ08 on 2016/7/28.
 */
@Service
public class SysPermissionsService {

    @Autowired
    SysPermissionsMapper sysPermissionsMapper;

    /**
     * 根据用户ID,获取权限列表
     *
     * @param userId
     * @return
     */
    public List<TreeNode> getPermissionsesByUserId(Long userId) {

        List<TreeNode> treeNodes = getTreeNodeByUserIdAndParentId(userId,-1l);

        return treeNodes;
    }

    public  List<TreeNode> getTreeNodeByUserIdAndParentId(Long userId,Long parentId){
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        HashMap params = new HashMap();
        params.put("userId",userId);
        params.put("parentId",parentId);
        List<SysPermissions> sysPermissionses = sysPermissionsMapper.getSysPermissionsByUserIdAndParentId(params);
        if(sysPermissionses.size()>0){
            for (int i=0;i<sysPermissionses.size();i++){
                TreeNode treeNode = new TreeNode();
                treeNode.setId(sysPermissionses.get(i).getId());
                treeNode.setText(sysPermissionses.get(i).getMenuname());
                if(sysPermissionses.get(i).getLeaf()!=1){
                    //存在子节点的情况下
                    treeNode.setChildren(getTreeNodeByUserIdAndParentId(userId,sysPermissionses.get(i).getId()));
                    treeNode.setState("closed");
                }
                treeNode.setAttributes(sysPermissionses.get(i));
                treeNodes.add(treeNode);
            }
        }
        return treeNodes;
    }

    /**
     * 只获取菜单
     *
     * @return
     */
    public List<SysPermissions> loadList() {
        List<SysPermissions> sysPermissionses = getPermissionsesByParentId(null);
        return sysPermissionses;
    }

    private List<SysPermissions> getPermissionsesByParentId(String parentId) {
        List<SysPermissions> sysPermissionses = sysPermissionsMapper.getSysPermissionsByParentId(parentId);
        if(sysPermissionses.size()>0) {
            for (int i = 0; i < sysPermissionses.size(); i++) {
                if(sysPermissionses.get(i).getLeaf()!=1){
                    //存在节点的情况下
                    sysPermissionses.get(i).setChildren(getPermissionsesByParentId(sysPermissionses.get(i).getId().toString()));
                }
            }
        }
        return sysPermissionses;
    }

    /**
     * 获取所有的列表
     *
     * @return
     */
    public List<SysPermissions> loadListAll() {
        List<SysPermissions> sysPermissionses = getPermissionsesAllByParentId(null);
        return sysPermissionses;
    }

    private List<SysPermissions> getPermissionsesAllByParentId(String parentId) {
        List<SysPermissions> sysPermissionses = sysPermissionsMapper.getSysPermissionsByParentId(parentId);
        if(sysPermissionses.size()>0) {
            for (int i = 0; i < sysPermissionses.size(); i++) {
                if(sysPermissionses.get(i).getLeaf()!=1){
                    //存在节点的情况下
                    sysPermissionses.get(i).setChildren(getPermissionsesAllByParentId(sysPermissionses.get(i).getId().toString()));
                }else{
                    //到最后的根节点的情况下，获取对应的Action
                    List<SysPermissions> actions = sysPermissionsMapper.getSysPermissionsActionAllByParentId(sysPermissionses.get(i).getId());
                    sysPermissionses.get(i).setChildren(actions);
                }
            }
        }
        return sysPermissionses;
    }

    public List<Map> selectAll() {
        return sysPermissionsMapper.selectAll();
    }
}
